package org.siqisource.webstart.core.widgets.tree.helper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.reflect.FieldUtils;
import org.apache.log4j.Logger;
import org.siqisource.webstart.core.widgets.tree.config.TreeConfig;
import org.siqisource.webstart.core.widgets.tree.constants.TreeNodeConstants;
import org.siqisource.webstart.core.widgets.tree.model.TreeNode;

public final class TreeHelper {

	/**
	 * 私有构造函数，防止类被实例化
	 */
	private TreeHelper() {

	}

	/** 日志 */
	private static final Logger LOGGER = Logger.getLogger(TreeHelper.class);

	/**
	 * 从节点内容中，获得节点结构需要的信息
	 * 
	 * @param treeMapping
	 *            fieldMapping
	 * @param node
	 *            node
	 * @return treeNode对象
	 */
	public static TreeNode getTreeNode(TreeConfig treeConfig, Object node) {
		TreeNode treeNode = new TreeNode();
		// 节点Id
		treeNode.setId(getId(treeConfig, node));
		// 父节点Id
		treeNode.setParentId(getParentId(treeConfig, node));
		// 节点标题
		treeNode.setName(getName(treeConfig, node));
		// 同级节点排列的序号
		treeNode.setSortNo(getSortNo(treeConfig, node));
		// 获得是否叶子节点
		treeNode.setIsLeaf(getIsLeaf(treeConfig, node));

		// 获得全路径
		treeNode.setFullPath(getFullPath(treeConfig, node));

		// 其余的属性
		List<String> properties = treeConfig.getOtherProperties();
		if (properties != null) {
			Map<String, Object> otherData = new HashMap<String, Object>();
			for (String property : properties) {
				Object objectValue = null;
				try {
					objectValue = FieldUtils.readField(node, property, true);
				} catch (IllegalAccessException e) {
					LOGGER.error(e);
				}
				otherData.put(property, objectValue);
			}
			treeNode.setOtherData(otherData);
		}

		return treeNode;
	}

	/**
	 * 从节点内容中，获得节点结构需要的信息
	 * 
	 * @param treeMapping
	 *            fieldMapping
	 * @param node
	 *            node
	 * @return treeNode对象
	 */
	public static TreeNode getTreeNodeFromMap(TreeConfig treeConfig, Map<String,Object> dataMap) {
		TreeNode treeNode = new TreeNode();
		// 节点Id
		treeNode.setId((Integer) dataMap.get(TreeNodeConstants.ID_KEY));
		// 父节点Id
		treeNode.setParentId((Integer) dataMap.get(TreeNodeConstants.PARENT_ID_KEY));
		// 节点标题
		treeNode.setName((String) dataMap.get(TreeNodeConstants.NAME_KEY));
		// 同级节点排列的序号
		treeNode.setSortNo((Integer) dataMap.get(TreeNodeConstants.SORT_NO_KEY));
		// 获得是否叶子节点
		treeNode.setIsLeaf((Boolean) dataMap.get(TreeNodeConstants.IS_LEAF_KEY));
		// 获得全路径
		treeNode.setFullPath((String) dataMap.get(TreeNodeConstants.FULL_PATH_KEY));

		// 其余的属性
		List<String> properties = treeConfig.getOtherProperties();
		if (properties != null) {
			Map<String, Object> otherData = new HashMap<String, Object>();
			for (String property : properties) {
				otherData.put(property, dataMap.get(property));
			}
		}

		return treeNode;
	}

	/**
	 * 获得节点Id
	 * 
	 * @param treeMapping
	 *            fieldMapping
	 * @param node
	 *            node
	 * @return 节点Id
	 */
	public static Integer getId(TreeConfig treeConfig, Object node) {
		// 节点Id
		Integer nodeId = null;
		try {
			nodeId = (Integer) FieldUtils.readField(node, treeConfig.getPropertyId(), true);
		} catch (IllegalAccessException e) {
			LOGGER.error(e);
		}
		return nodeId;
	}

	/**
	 * 获得父节点Id
	 * 
	 * @param treeMapping
	 *            fieldMapping
	 * @param node
	 *            node
	 * @return 返回父节点Id
	 */
	public static Integer getParentId(TreeConfig treeConfig, Object node) {
		Integer parentNodeId = null;
		try {
			parentNodeId = (Integer) FieldUtils.readField(node, treeConfig.getPropertyParentId(), true);
		} catch (IllegalAccessException e) {
			LOGGER.error(e);
		}
		return parentNodeId;
	}

	/**
	 * 获得节点标题
	 * 
	 * @param treeMapping
	 *            fieldMapping
	 * @param node
	 *            node
	 * @return 返回及诶单标题
	 */
	public static String getName(TreeConfig treeConfig, Object node) {
		String nodeLabel = null;
		try {
			nodeLabel = (String) FieldUtils.readField(node, treeConfig.getPropertyName(), true);
		} catch (IllegalAccessException e) {
			LOGGER.error(e);
		}
		return nodeLabel;
	}

	/**
	 * 获得序号
	 * 
	 * @param treeMapping
	 *            fieldMapping
	 * @param node
	 *            node
	 * @return 返回父节点Id
	 */
	public static Integer getSortNo(TreeConfig treeConfig, Object node) {
		Integer sortNo = 0;
		try {
			sortNo = (Integer) FieldUtils.readField(node, treeConfig.getPropertySortNo(), true);
		} catch (IllegalAccessException e) {
			LOGGER.error(e);
		}
		return sortNo;
	}

	/**
	 * 获得是否叶子节点
	 * 
	 * @param treeMapping
	 *            fieldMapping
	 * @param node
	 *            node
	 * @return 返回父节点Id
	 */
	public static Boolean getIsLeaf(TreeConfig treeConfig, Object node) {
		Boolean isLeaf = true;
		try {
			isLeaf = (Boolean) FieldUtils.readField(node, treeConfig.getPropertyIsLeaf(), true);
		} catch (IllegalAccessException e) {
			LOGGER.error(e);
		}
		return isLeaf;
	}

	/**
	 * 获得全路径
	 * 
	 * @param treeMapping
	 *            fieldMapping
	 * @param node
	 *            node
	 * @return 返回父节点Id
	 */
	public static String getFullPath(TreeConfig treeConfig, Object node) {
		String fullPath = "";
		try {
			fullPath = (String) FieldUtils.readField(node, treeConfig.getPropertyFullPath(), true);
		} catch (IllegalAccessException e) {
			LOGGER.error(e);
		}
		return fullPath;
	}

}
